<template>
  <a-modal
    title="注销"
    v-model="show"
    :maskClosable="false"
    :width="1050"
    placement="right"
    :closable="true"
    @cancel="onClose"
    :footer="null"
    :visiable="devBindInfoCancelVisiable"
  >
    <a-form :form="form">
      <a-row>
        <a-col :md="12" :sm="24">
          <a-form-item label="设备唯一编号"
                       v-bind="formItemLayout">
            <a-input v-decorator="['bindDevId']" :disabled="true"/>
          </a-form-item>
          <a-form-item label="id"
                       v-bind="formItemLayout" style="display: none">
            <a-input v-decorator="['bindDevType']"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item label='产品/品牌代码'
                       v-bind="formItemLayout">
            <a-input v-decorator="['bindDevBrand']"
                     :disabled="true"/>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :md="12" :sm="24">
          <a-form-item label="设备名称"
                       v-bind="formItemLayout">
            <a-input v-decorator="['bindDevModel']" :disabled="true"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item label="推送渠道开关"
                       v-bind="formItemLayout">
            <a-select
              mode="default"
              :allowClear="true"
              style="width: 100%"
              show-search
              placeholder="请选择"
              option-filter-prop="children"
              :filter-option="filterOption"
              :disabled="true"
              v-decorator="['msgPushFlag']">
              <a-select-option key="Y">开</a-select-option>
              <a-select-option key="N">关</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :md="12" :sm="24">
          <a-form-item label="绑定类型"
                       v-bind="formItemLayout">
            <a-select
              mode="default"
              :allowClear="true"
              style="width: 100%"
              show-search
              placeholder="请选择"
              option-filter-prop="children"
              :filter-option="filterOption"
              :disabled="true"
              v-decorator="['bindType']">
              <a-select-option key="00">收银员</a-select-option>
              <a-select-option key="01">商户</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item label="绑定商户"
                       v-bind="formItemLayout">
            <a-select
              show-search
              placeholder="请输入代码或名称"
              option-filter-prop="children"
              style="width: 334px"
              :filter-option="filterOption"
              @focus="selectBindMchntN"
              @search="selectBindMchntN"
              :disabled="true"
              v-decorator="['mchntNo']"
            >
              <a-select-option v-for="(d,index) in bindMchntNData" :key="d.mchntNo">
                {{ d.mchntNo }}-{{d.mchntName}}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :md="12" :sm="24">
          <a-form-item label="推送渠道"
                       v-bind="formItemLayout">
            <a-select
              mode="default"
              :allowClear="true"
              style="width: 100%"
              show-search
              placeholder="请选择"
              option-filter-prop="children"
              :filter-option="filterOption"
              :disabled="true"
              v-decorator="['msgPushKey']">
              <a-select-option key="01">阿里云</a-select-option>
              <a-select-option key="02">信雅达</a-select-option>
              <a-select-option key="03">意锐</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item label="注销原因"
                       v-bind="formItemLayout">
            <a-input v-decorator="['cancelReason',{rules: [{ required: true, message: '必填字段'}]}]"/>
          </a-form-item>
        </a-col>
      </a-row>
      <br>

    </a-form>
    <div class="drawer-bootom-button">
      <a-popconfirm title="确定放弃编辑？" @confirm="onClose" okText="确定" cancelText="取消">
        <a-button style="margin-right: .8rem">取消</a-button>
      </a-popconfirm>
      <a-button @click="handleSubmit" type="primary" :loading="loading">提交</a-button>
    </div>
  </a-modal>
</template>
<script>
  const formItemLayout = {
    labelCol: {span: 8},
    wrapperCol: {span: 16}
  }
  export default {
    name: 'devBindInfoCancel',
    props: {
      devBindInfoCancelVisiable: {
        default: false
      },
      instId: '',
    },
    data() {
      return {
        bindMchntNData: [],
        loading: false,
        formItemLayout,
        form: this.$form.createForm(this),
        validateStatus: '',
        // firstValue: {},
        info: {
          infoName: '',
          remark: '',
          menuId: ''
        }
      }
    },
    computed: {
      show: {
        get: function () {
          return this.devBindInfoCancelVisiable
        },
        set: function () {
        }
      }
    },
    mounted() {

    },

    methods: {

      selectBindMchntN() {
        this.bindMchntNData = []
        // console.log(this.instId)
        this.$get('admin/mcmp/bMchntBaseInfo/getMchntBaseInfo', {instId: this.instId}).then((r) => {
          let result = r.data
          result.forEach(r => {
            this.bindMchntNData.push(r);
          })
        })
      },
      filterOption(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },

      reset() {
        this.loading = false
        this.form.resetFields()
      },
      onClose() {
        this.reset()
        this.$emit('close')
      },
      handleSubmit() {
        this.form.validateFields((err, values) => {
          if (!err) {
            this.setInfoFields()
            this.loading = true
            this.info.instId = this.instId
            this.info.statusChangeFlag = 'cancel'
            // console.log(this.info)
            this.$put('devBindInfo/bindInfoCancel', {
              ...this.info
            }).then((r) => {
              this.reset()
              this.$emit('success')
            }).catch(() => {
              this.loading = false
            })
          }
        })
      },

      setInfoFields() {
        let values = this.form.getFieldsValue(['bindDevId', 'bindDevBrand', 'bindDevModel', 'msgPushFlag',
          'bindType', 'mchntNo', 'msgPushKey', 'bindDevType', 'cancelReason'])
        if (typeof values !== 'undefined') {
          Object.keys(values).forEach(_key => {
            this.info[_key] = values[_key]
          })
        }
      },

      setDevBindInfoFormValues ({...info}) {
        let fields = ['bindDevId', 'bindDevBrand', 'bindDevModel', 'msgPushFlag', 'bindType', 'mchntNo', 'msgPushKey', 'bindDevType']
        Object.keys(info).forEach((key) => {
          if (fields.indexOf(key) !== -1) {
            this.form.getFieldDecorator(key)
            let obj = {}
            obj[key] = info[key]
            this.form.setFieldsValue(obj)
          }
        })
      },


    }

  }
</script>



